package com.xingongchang.baolebei.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.CategoryActivity;
import com.xingongchang.baolebei.activity.ProductListActivity;
import com.xingongchang.baolebei.model.HomeModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.SearchModel;
import com.xingongchang.baolebei.protocol.CATEGORY;
import com.xingongchang.baolebei.protocol.FILTER;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class FeiLeiFragment extends Fragment implements BusinessResponse,OnClickListener{
	private TextView view1,view2,view3,view4,view5,view6
	,view7,view8,view9,view10;
	private RelativeLayout layout,layout2,layout3,layout4,layout5;
	View view;
	private HomeModel homeModel;
	private SearchModel searchModel;
	ImageView imageView;
	EditText input;
   @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.fenlie, null);
		homeModel=new HomeModel(getActivity());
		homeModel.addResponseListener(this);
		homeModel.fei();
		searchModel=new SearchModel(getActivity());
		searchModel.searchCategory();
		imageView=(ImageView) view.findViewById(R.id.top_view_search);
		input = (EditText) view.findViewById(R.id.search_input);
		input.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {

				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					try {
						Intent intent = new Intent(getActivity(),
								ProductListActivity.class);
						FILTER filter = new FILTER();
						filter.keywords = input.getText().toString().toString();
						intent.putExtra(ProductListActivity.FILTER, filter
								.toJson().toString());
						startActivity(intent);
						getActivity().overridePendingTransition(
								R.anim.push_right_in, R.anim.push_right_out);

					} catch (JSONException e) {

					}
				}
				return false;
			}
		});
		imageView.setOnClickListener(this);
		intiview();
		return view;
	}
	private void intiview() {
		view1=(TextView) view.findViewById(R.id.h1);
		view2=(TextView) view.findViewById(R.id.h2);
		view3=(TextView) view.findViewById(R.id.h3);
		view4=(TextView) view.findViewById(R.id.h4);
		view5=(TextView) view.findViewById(R.id.h5);
		view6=(TextView) view.findViewById(R.id.h6);
		view7=(TextView) view.findViewById(R.id.h7);
		view8=(TextView) view.findViewById(R.id.h8);
		view9=(TextView) view.findViewById(R.id.h9);
		view10=(TextView) view.findViewById(R.id.h10);
		layout=(RelativeLayout) view.findViewById(R.id.hbdt);
		layout2=(RelativeLayout) view.findViewById(R.id.hbdt1);
		layout3=(RelativeLayout) view.findViewById(R.id.hbdt2);
		layout4=(RelativeLayout) view.findViewById(R.id.hbdt3);
		layout5=(RelativeLayout) view.findViewById(R.id.hbdt4);
		layout.setOnClickListener(this);
		layout2.setOnClickListener(this);
		layout3.setOnClickListener(this);
		layout4.setOnClickListener(this);
	    layout5.setVisibility(View.GONE);
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ProtocolConst.fenlei)) {
			view1.setText(homeModel.fen.get(0).gname.toString());
			view2.setText(homeModel.fen.get(0).gthumb.toString());
			view3.setText(homeModel.fen.get(1).gname.toString());
			view4.setText(homeModel.fen.get(1).gthumb.toString());
			view5.setText(homeModel.fen.get(2).gname.toString());
			view6.setText(homeModel.fen.get(2).gthumb.toString());
			view7.setText(homeModel.fen.get(3).gname.toString());
			view8.setText(homeModel.fen.get(3).gthumb.toString());
			view9.setText(homeModel.fen.get(4).gname.toString());
			view10.setText(homeModel.fen.get(4).gthumb.toString());
		} 
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_view_search:
			try {
				Intent intent = new Intent(getActivity(),
						ProductListActivity.class);
				FILTER filter = new FILTER();
				filter.keywords = input.getText().toString().toString();
				intent.putExtra(ProductListActivity.FILTER, filter.toJson()
						.toString());
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);

			} catch (JSONException e) {

			}
			break;
		case R.id.hbdt:
				CATEGORY category = searchModel.categoryArrayList
						.get(0);
				//CATEGORY item = category.children.get(0);
						Intent it = new Intent(getActivity(),
								ProductListActivity.class);
						try {
							    FILTER filter = new FILTER();
	                            filter.category_id = String.valueOf(homeModel.fen.get(0).gid);
	                            it.putExtra(ProductListActivity.FILTER,filter.toJson().toString());
//							it.putExtra("category", category.toJson()
//										.toString());
//							it.putExtra("category_name",
//									searchModel.categoryArrayList
//											.get(0).name);
							startActivity(it);
							getActivity().overridePendingTransition(
									R.anim.push_right_in,
									R.anim.push_right_out);
						} catch (JSONException e) {
							e.printStackTrace();
						}
//			}
			break;
		case R.id.hbdt1:
			//CATEGORY catego = searchModel.categoryArrayList
			//.get(1);
			Intent i = new Intent(getActivity(),
					ProductListActivity.class);
			try {
				 FILTER filter = new FILTER();
                 filter.category_id = String.valueOf(homeModel.fen.get(1).gid);
                 i.putExtra(ProductListActivity.FILTER,filter.toJson().toString());
//					i.putExtra("category", catego.toJson()
//							.toString());
//				i.putExtra("category_name",
//						searchModel.categoryArrayList
//								.get(1).name);
				startActivity(i);
				getActivity().overridePendingTransition(
						R.anim.push_right_in,
						R.anim.push_right_out);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case R.id.hbdt2:
			//CATEGORY category1 = searchModel.categoryArrayList
			//.get(2);
			Intent it1 = new Intent(getActivity(),
					ProductListActivity.class);
			try {
				 FILTER filter = new FILTER();
                 filter.category_id = String.valueOf(homeModel.fen.get(2).gid);
                 it1.putExtra(ProductListActivity.FILTER,filter.toJson().toString());
//					it1.putExtra("category", category1.toJson()
//							.toString());
//				it1.putExtra("category_name",
//						searchModel.categoryArrayList
//								.get(2).name);
				startActivity(it1);
				getActivity().overridePendingTransition(
						R.anim.push_right_in,
						R.anim.push_right_out);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case R.id.hbdt3:
			//CATEGORY category11 = searchModel.categoryArrayList
			//.get(3);
			Intent it11 = new Intent(getActivity(),
					ProductListActivity.class);
			try {
				 FILTER filter = new FILTER();
                 filter.category_id = String.valueOf(homeModel.fen.get(3).gid);
                 it11.putExtra(ProductListActivity.FILTER,filter.toJson().toString());
//					it11.putExtra("category", category11.toJson()
//							.toString());
//				it11.putExtra("category_name",
//						searchModel.categoryArrayList
//								.get(3).name);
				startActivity(it11);
				getActivity().overridePendingTransition(
						R.anim.push_right_in,
						R.anim.push_right_out);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
//		case R.id.hbdt4:
//			//CATEGORY category3 = searchModel.categoryArrayList
//			//.get(4);
//			Intent it3 = new Intent(getActivity(),
//					ProductListActivity.class);
//			try {
//				 FILTER filter = new FILTER();
//                 filter.category_id = String.valueOf(searchModel.categoryArrayList.get(0).id);
//                 it3.putExtra(ProductListActivity.FILTER,filter.toJson().toString());
////					it3.putExtra("category", category3.toJson()
////							.toString());
////				it3.putExtra("category_name",
////						searchModel.categoryArrayList
////								.get(4).name);
//				startActivity(it3);
//				getActivity().overridePendingTransition(
//						R.anim.push_right_in,
//						R.anim.push_right_out);
//			} catch (JSONException e) {
//				e.printStackTrace();
//			}
//			break;
		}
	}
}
